<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 2017/12/23
 * Time: 下午4:58
 */

namespace Biz\module\Classes\adm;

use Biz\model\admin\AdminUser as mShopAdminUser;
use Biz\model\ShopBasicInfo as mShopBasicInfo;
use Biz\model\ShopSecurityDepositLog as mShopSecurityDepositLog;
use Biz\module\Classes\core\SearchListAbstract;


final class SecurityDepositSearch extends SearchListAbstract
{

    protected function buildSelect()
    {
        $this->select = [
            'id',
        ];
    }

    protected function buildWhere()
    {
        //状态
        if ($this->params['status'] > -1) {
            $this->where['sbi.status'] = $this->params['status'];
        }

        //类型
        if ($this->params['type'] > -1) {
            $this->where['type'] = $this->params['type'];
        }

        if ($this->params['start_date']) {
            $this->where['addtime[>]'] = $this->params['start_date'];
        }


        if ($this->params['stop_date']) {
            $this->where['addtime[<]'] = date('Y-m-d', strtotime($this->params['stop_date']) + 86400);
        }


        //自定义搜索条件
        if (!empty($this->params['field'])) {
            if (!empty($this->params['keyword'])) {
                if (in_array($this->params['field'], ['uid'])) {
                    $this->where[$this->params['field']] = $this->params['keyword'];
                }
            }

        }

        if ($this->params['operator'] == -1) {
            $this->where['balance[<]'] = 0;
        } elseif ($this->params['operator'] == 1) {
            $this->where['balance[>]'] = 0;
        }

    }

    public function getCount()
    {
        $count = mShopSecurityDepositLog::count($this->where);

        return $count;
    }

    public function getList()
    {

        $list = mShopSecurityDepositLog::select($this->select, array_merge($this->where, ['ORDER'=>['id'=>'DESC'],'LIMIT' => $this->limit]));


        if ($list) {

            $ids = array_column($list, 'id');

            $logList = mShopSecurityDepositLog::getLogList($ids);

            $uids = array_column($logList, 'uid');

            if ($uids) {
                $shopList = mShopBasicInfo::getInfoList($uids);
            }

            $adminUids = array_column($logList, 'adminuid');

            if ($adminUids) {
                $adminUsers = mShopAdminUser::getAdminList($adminUids);
            }

            if ($logList) {
                foreach ($logList as &$log) {
                    $log['shop'] = isset($shopList[$log['uid']]) ? $shopList[$log['uid']] : [];
                    $log['admin'] = isset($adminUsers[$log['adminuid']]) ? $adminUsers[$log['adminuid']] : [];
                }
            }


            return $logList;
        }
    }

}
